package com.example.lt.controller;

import com.example.lt.constant.MessageConstant;
import com.example.lt.constant.PicPathConstant;
import com.example.lt.pojo.entity.Singer;
import com.example.lt.pojo.vo.SingerVO;
import com.example.lt.result.Result;
import com.example.lt.service.SingerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 小涛Tao
 * @Date: 2024/05/31/15:55
 * version 1.0 注释：
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/singer")
public class SingerController {

    private final SingerService singerService;

    /**
     * 管理端添加新的歌手
     * @param singer
     * @return
     */
    @PostMapping("/admin/add")
    public Result addSinger(@RequestBody Singer singer) {
        log.info("添加新的歌手：{}",singer);
        singer.setPic(PicPathConstant.DEFAULT_SINGER_PIC);
        boolean flag = singerService.save(singer); // 添加歌手
        if (flag) {
            return Result.success(MessageConstant.SINGER_ADD_SUCCESS);
        } else {
            return Result.error(MessageConstant.SINGER_ADD_FAIL);
        }
    }

    /**
     * 管理端更新歌手信息
     * @param singer
     * @return
     */
    @PostMapping("/admin/update")
    public Result updateSinger(@RequestBody Singer singer) {
        log.info("更新歌手信息：{}",singer);
        boolean flag = singerService.updateById(singer);
        if (flag) {
            return Result.success(MessageConstant.SINGER_UPDATE_SUCCESS);
        } else {
            return Result.error(MessageConstant.SINGER_UPDATE_FAIL);
        }
    }

    /**
     * 更新歌手图片
     * @param picFile
     * @param id
     * @return
     */
    @PostMapping("/admin/singerPic")
    public Result updateSingerPic(@RequestParam("file") MultipartFile picFile, @RequestParam("id") int id) {
        log.info("更新歌手图片：{}",id);
        return singerService.updateSingerPic(picFile,id);
    }

    /**
     * 管理端删除歌手
     * @param id
     * @return
     */
    @DeleteMapping("/admin/delete")
    public Result deleteSinger(@RequestParam Integer id) {
        log.info("管理端删除歌手：{}", id);
        boolean flag = singerService.deleteSinger(id);
        if (flag) {
            return Result.success(MessageConstant.DELETE_SUCCESS);
        }
        return Result.error(MessageConstant.DELETE_FAIL);
    }

    /**
     * 获取所有歌手的名字和图片路径，获取的信息很少
     * @return
     */
    @GetMapping("/common/getSingerPicPath")
    public Result<List<SingerVO>> getSingerPicPath() {
        log.info("获取歌手的名字和图片路径");
        List<SingerVO> singerVOList = singerService.getSingerPicPath();
        return Result.success(singerVOList);
    }

    /**
     * 获取所有歌手信息，获取歌曲的大量信息
     * @return
     */
    @GetMapping("/common/getAllSinger")
    public Result<List<Singer>> getAllSinger() {
        log.info("获取所有歌手信息");
        List<Singer> singerList = singerService.list();
        return Result.success(singerList);
    }

    /**
     * 根据性别获取歌手信息
     * @param sex
     * @return
     */
    @GetMapping("/common/sex")
    public Result getSingerBySex(@RequestParam int sex) {
        log.info("根据性别获取歌手信息：{}",sex);
        return singerService.getSingerBySex(sex);
    }

}
